﻿using UnityEngine;

public class Projectile : MonoBehaviour
{
    public BaseItemScript emitter, goal;
    public enum MotionType { Linear, Parabolic, Tracking ,Brownian}
    public MotionType motionType = MotionType.Linear;
    private Transform target;
    [SerializeField]private float speed = 10f;
    [SerializeField] private float trackingSpeed = 5f;
    [SerializeField] private float brownianFrequency = 0.5f;
    [SerializeField] private float brownianIntensity = 0.5f;
    [SerializeField] private float brownianTimer = 0f;
    [SerializeField] private Vector3 brownianDirection;

    private Vector3 startPosition;
    private Vector3 targetPosition;
    private bool hasTarget = false;
    private bool attached = false;
    public void SetData(BaseItemScript self, BaseItemScript goal)
    {
        this.emitter = self;
        this.goal = goal;
        this.target = goal.transform;

        startPosition = self.transform.position;
        targetPosition = goal.transform.position;
        hasTarget = true;
    }

    void Update()
    {
        if (!hasTarget) return;

        switch (motionType)
        {
            case MotionType.Linear:
                LinearMotion();
                break;
            case MotionType.Parabolic:
                ParabolicMotion();
                break;
            case MotionType.Tracking:
                TrackingMotion();
                break;
            case MotionType.Brownian:
                BrownianMotion();
                break;
        }
    }

    void LinearMotion()
    {
        Vector3 direction = (targetPosition - transform.position).normalized;
        Vector3 targetPos = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

            transform.position = targetPos;

        // 检测是否到达目标
        if (Vector3.Distance(transform.position, targetPosition) < 0.1f)
        {
            OnReachTarget();
        }
    }

    void ParabolicMotion()
    {
        float distance = Vector3.Distance(startPosition, targetPosition);
        float maxHeight = distance / 2f;
        float t = Vector3.Distance(startPosition, transform.position) / distance;
        float height = maxHeight * (1 - (2f * t - 1f) * (2f * t - 1f));

        Vector3 newPos = Vector3.Lerp(startPosition, targetPosition, t);
        newPos.y += height;

        
            transform.position = newPos;

        // 检测是否到达目标
        if (t >= 1f)
        {
            OnReachTarget();
        }
    }

    void TrackingMotion()
    {
        if (target == null) return;

        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(direction);

        // 在TrackingMotion中添加：
        float distanceFactor = Mathf.Clamp01(Vector3.Distance(transform.position, targetPosition) / 50f);
        var velocity = Mathf.Lerp(2f, 10f, distanceFactor);
        // 平滑旋转
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, velocity * Time.deltaTime);

        
        transform.position += transform.forward * speed * Time.deltaTime;

        
    }
    void BrownianMotion()
    {
        if (target == null) return;
        // 更新目标位置
        targetPosition = target.position;
        // 更新布朗运动方向
        brownianTimer += Time.deltaTime;
        if (brownianTimer >= brownianFrequency)
        {
            ResetBrownianDirection();
            brownianTimer = 0;
        }
        // 计算主要方向（朝向目标）
        Vector3 mainDirection = (targetPosition - transform.position).normalized;

        // 混合主要方向和随机方向
        Vector3 finalDirection = Vector3.Lerp(
            mainDirection,
            brownianDirection,
            brownianIntensity
        ).normalized;
        // 移动
            transform.position += finalDirection * speed * Time.deltaTime;
        
    }
    void ResetBrownianDirection()
    {
        // 生成随机方向（排除完全相反的方向）
        Vector3 randomDirection;
        do
        {
            randomDirection = Random.insideUnitSphere;
        }
        while (Vector3.Dot(randomDirection, transform.forward) < -0.5f);

        brownianDirection = randomDirection.normalized;
    }

    void OnReachTarget()
    {
        if (!attached)
        {
            attached = true;

            // 到达目标时的处理（例如爆炸效果）
            Debug.Log("Target Reached!");
            goal.OnReceiveHit(emitter);
        }
    }

}